#include <stdio.h>
#include <sqlite3.h>
#include <time.h>
#include <string.h>

#define size 1024

/*全局变量*/
char username[20],password[50];
char* lastlogin;
sqlite3* pdb;
char sql[size],*errmsg;
char** result;
int row,col;

/*打印登录界面*/
void printfUI(void)
{
	printf("输入\t命令\n");
	printf("1\t登录\n");
	printf("2\t注册\n");
	printf("0\t退出\n");
}

void gettime(char** ptime)
{
	char curtime[20];
  time_t tm;
	struct tm *p;
	
  time(&tm);
  p = localtime(&tm);
  snprintf(curtime, 20, "%d-%d-%d %2d:%2d:%2d", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
	*ptime = curtime;
}

void login(void)
{
	printf("--------------------登录--------------------\n");
	printf("用户名：");
	scanf("%s",username);
	fflush(stdin);
	printf("密码：");
	scanf("%s",password);
	fflush(stdin);

	snprintf(sql,size,"select * from users \
		where username = '%s' and password = '%s';",username,password);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	if(row == 0)
		printf("登录失败。\n用户名与密码不匹配。\n");
	else
	{
		gettime(&lastlogin);
		snprintf(sql,size,"update users set lastlogin = '%s'\
			where username = '%s';",lastlogin,username);
		sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);
		printf("登录成功！\n");
	}
}

void reg(void)
{
	char password2[50];

	printf("--------------------注册--------------------\n");
	printf("用户名：");
	scanf("%s",username);
	fflush(stdin);
	printf("密码：");
	scanf("%s",password);
	fflush(stdin);
	REPWD:
	printf("请再次输入密码：");
	scanf("%s",password2);
	fflush(stdin);

	if(strncmp(password,password2,50) != 0)
	{
		printf("两次输入的密码不一致，请重新输入。\n");
		goto REPWD;
	}

	/*查询用户是否已注册*/
	snprintf(sql,size,"select * from users \
		where username = '%s';",username);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	if(row > 0)
	{
		printf("注册失败。\n用户已存在。\n");
		return;
	}

	/*添加用户*/
	gettime(&lastlogin);
	snprintf(sql,size,"insert into users values('%s','%s','%s');",username,password,lastlogin);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);
	printf("注册成功！\n");
}

int main(void)
{
	int inputcode;
	char* logintime;
	
	/*创建数据库和用户表*/
	sqlite3_open("user.db",&pdb);
	strncpy(sql,"create table if not exists users (\
		username char[20] primary key,\
		password char[50],\
		lastlogin char[20]);",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);
	
	/*添加初始用户*/
	strncpy(sql,"insert into users values('root','123','2024-9-13 16:55:41');",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);
	strncpy(sql,"insert into users values('robin','123','2024-9-13 16:55:41');",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);

	/*用户界面*/
	START:
	printfUI();
	gettime(&logintime);
	printf("当前时间：%s\n",logintime);
	printf(">>");
	scanf("%d",&inputcode);

	if(inputcode == 0)
		return 0;
	else if(inputcode == 1)
		login();
	else if(inputcode == 2)
		reg();
	else
	{
		printf("输入的命令无法识别，请重新输入。\n");
		goto START;
	}

	/*关闭数据库*/
	sqlite3_close(pdb);

	return 0;
}

